package edu.njust.cma.service.impl;

import edu.njust.cma.mapper.UserMapper;
import edu.njust.cma.pojo.User;
import edu.njust.cma.service.UserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public User getUserInfo(Integer id) {
        return userMapper.getUserInfo(id);
    }

    @Override
    public void changePassword(Integer id, String password) {
        userMapper.changePassword(id, password);
    }

    @Override
    public void changeUserInfo(Integer id, String name, String email, String phone) {
        userMapper.changeUserInfo(id, name, email, phone);
    }

    @Override
    public List<User> getAllUserInfo() {
        return userMapper.getAllUserInfo();
    }

    @Override
    public void changeResume(Integer id, String resumeUrl) {
        userMapper.changeResume(id, resumeUrl);
    }

    @Override
    public List<User> getUserInfoByLevel(Integer level) {
        return userMapper.getUserInfoByLevel(level);
    }

    @Override
    public void changeUserLevel(Integer userId, Integer level) {
        userMapper.changeUserLevel(userId, level);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUserById(userId);
    }

    @Override
    public User addUser(User user) {
        String maxWorkNumber = userMapper.getMaxWorkNumber();
        int maxWorkNumberInt = Integer.parseInt(maxWorkNumber);
        maxWorkNumberInt++;
        // 修改为4位数 ，高位补0的字符串
        String maxWorkNumberString = String.format("%04d", maxWorkNumberInt);
        user.setWorkNumber(maxWorkNumberString);
        userMapper.addUser(user);
        user = userMapper.getUserByWorkNumber(maxWorkNumberString);
        return user;
    }

    @Override
    public User login(String work_number, String password) {
        User user =userMapper.getUserByWorkNumber(work_number);
        if(user.getPassword() .equals(password)){
            return user;
        }else {
            return null;
        }
    }

}
